﻿using SaaS.Model.Dto.Users;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using XCore.Redis;

namespace SaaS.Cache.Users
{
    public class UserCacheService : CacheServiceBase
    {
        private const string USER_INFO = "USER_INFO:";
        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <param name="user"></param>
        public void SaveUserInfo(UserModel user)
        {
            string key = USER_INFO + user.Id;
            Redis.HashSetAsync(key, user);
        }
        /// <summary>
        /// 获取用户所有字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserModel GetUserInfo(int id)
        {
            string key = USER_INFO + id;
            return Redis.HashGetAll(key);
        }
        /// <summary>
        /// 获取用户某个字段
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="id"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public TValue GetUserInfo<TValue>(int id, Expression<Func<UserModel, TValue>> exp)
        {
            string key = USER_INFO + id;
            string dataKey = exp.ToString();
            return Redis.HashGet(key, dataKey).GetRedisValue<TValue>();
        }
        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CheckUser(int id)
        {
            string key = USER_INFO + id;
            return Redis.KeyExists(key);
        }
    }
}
